#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>

int main(int argc, char const *argv[])
{
    int fd1 = open("./love1.txt",O_RDWR | O_CREAT,0666);
    if (fd1 == -1)
    {
        perror("打开love1.txt失败");
        exit(-1);
    }
    
    int fd2 = open("./love2.txt",O_RDWR | O_CREAT,0666);
    if (fd2 == -1)
    {
        perror("打开love2.txt失败");
        exit(-1);
    }

    //读取fd1中的内容
    char buf[1024];
    ssize_t read_bytes;
    while ((read_bytes = read(fd1, buf, sizeof(buf))) > 0)
    {
        //STDOUT_FILENO 标准输出，控制台
        write(STDOUT_FILENO,buf,read_bytes);
        //将读取到的内容写入到fd2中
        write(fd2,buf,read_bytes);
    }

    //判断一旦读取失败
    if (read_bytes == -1)
    {
        perror("读取文件失败\n");
        close(fd1);
        close(fd2);
        exit(-1);
    }
    close(fd1);
    close(fd2);

    return 0;
}
